'use strict';

/**
 * @ngdoc function
 * @name webApp.controller:UsercollectionCtrl
 * @description
 * # UsercollectionCtrl
 * Controller of the webApp
 */

angular.module('webApp')
  .controller('userCollection', function ($scope,promiseUserCollection,$timeout,cookie,$ionicScrollDelegate) {
  $scope.$on('$stateChangeSuccess',function(){
    if(cookie.getCookie('access_token')){
      var page = 1;
      $scope.pageShow = true;
      promiseUserCollection.userCollection(page,10).then(function(data){
        $scope.userCollectionData = data;
      });
      $scope.setId = function(id){
        cookie.setCookie('newsId',id);
      };
      //翻页
      $scope.loadMore = function(){
        page += 1;
        promiseUserCollection.userCollection(page,10).then(function(data){
          $scope.pageShow = false;
          $timeout(function(){
            if(data.length == 0){
              $scope.pageShow = false;
            }
            else{
              $scope.pageShow = true;
            }
          });
          for(var i = 0;i < data.length;i++){

            $scope.userCollectionData.splice($scope.userCollectionData.length + i,0,data[i]);
          }
          $ionicScrollDelegate.resize();
        });
      };
    }
  });
});
